/**
 * 
 */
package org.tuestudent.grouppa;

import java.io.ByteArrayOutputStream;

/**
 * Contains messages that are to be sent to the server
 * 
 * @author s121170
 * 
 */
public class MessageFromApp {

    private static int lastId = 0;

    public MessageFromApp() {
        id = ++lastId;
        AppContext ctx = Main.getContext();
        if (ctx.currentAccessToken != null)
            accessToken = ctx.currentAccessToken;
        if (ctx.getCurrentGroup() != null)
            toGroup = ctx.getCurrentGroup();
    }

    /**
     * to which group the message is intended
     */
    public String toGroup = "";
    /**
     * which task to execute on controller on server
     */
    public String task = "";
    /**
     * arguments serialized in JSON
     */
    public String serializedArguments = "";
    /**
     * username of user that sent this message
     */
    public String from = "";
    /**
     * id of the message is automatically assigned by the constructor
     */
    public long id = -1;
    /**
     * name of the controller on the server to which message is intended
     */
    public String toController = "";
    /**
     * used for identifying response to this message the response message will
     * have the same message type
     */
    public String messageType = "";

    /**
     * access token in case the user is already logged in
     */
    public String accessToken = "";

    /**
     * returns a JSON version of this message
     * 
     * @return string with json encoded this
     */
    public String getJSON() {
        // CHECK: return a JSON version of this message
        return "{\"toGroup\":\""
                + ((toGroup.isEmpty()) ? "" : toGroup)
                + "\",\"task\":\""
                + ((task.isEmpty()) ? "" : task)
                + "\",\"serializedArguments\":"
                + ((serializedArguments.isEmpty()) ? "\"\""
                        : serializedArguments) + ",\"from\":\""
                + ((from.isEmpty()) ? "" : from) + "\",\"id\":" + id
                + ",\"toController\":\""
                + ((toController.isEmpty()) ? "" : toController)
                + "\",\"messageType\":\""
                + ((messageType.isEmpty()) ? "" : messageType)
                + "\",\"accessToken\":\""
                + ((accessToken.isEmpty()) ? "" : accessToken) + "\"}";
    }
}
